package com.dpi.cgdc.data.model.vo;

import com.dpi.cgdc.data.enums.ErrorCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @Description: web结果对象
 * @return
 * @Author leipeng
 * @Date 2022/11/10 11:19
 **/
@ApiModel(value = "响应结果对象", description = "响应结果对象")
public final class ResultVo<T> implements Serializable {

    private static final long serialVersionUID = 2497291457221049903L;

    @ApiModelProperty(value = "响应结果数据")
    private T data;
    @ApiModelProperty(value = "响应信息")
    private String resMsg;
    @ApiModelProperty(value = "响应码")
    private String resCode;

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getResMsg() {
        return this.resMsg;
    }

    public void setResMsg(String resMsg) {
        this.resMsg = resMsg;
    }

    public String getResCode() {
        return this.resCode;
    }

    public void setResCode(String resCode) {
        this.resCode = resCode;
    }

    private ResultVo() {
    }

    public static ResultVo newInstance() {
        return new ResultVo();
    }

    /**
     * 初始化一个新创建的 Result 对象
     *
     * @param resCode 返回码
     * @param resMsg  返回内容
     * @param data    数据对象
     */
    public ResultVo(String resCode, String resMsg, T data) {
        this.data = data;
        this.resMsg = resMsg;
        this.resCode = resCode;
    }

    public static ResultVo successDefault() {
        ResultVo result = new ResultVo();
        result.setResCode(ErrorCodeEnum.RESCODE_SUCCESS.getCode());
        result.setResMsg(ErrorCodeEnum.RESCODE_SUCCESS.getCodeMsg());
        return result.success((Object) "true");
    }

    public static ResultVo successData(Object data) {
        ResultVo Result = newInstance();
        return Result.success(data);
    }

    public ResultVo success() {
        return this.success((T) "true");
    }

    public ResultVo success(T data) {
        return this.success(data, "操作成功");
    }

    public static ResultVo successMsg(String resMsg) {
        ResultVo result = newInstance();
        result.setResCode(ErrorCodeEnum.RESCODE_SUCCESS.getCode());
        return result.success(resMsg);
    }

    public ResultVo success(String resMsg) {
        return this.success((T) "true", resMsg);
    }

    public static ResultVo successDataMsg(Object data, String resMsg) {
        ResultVo result = newInstance();
        result.setResCode(ErrorCodeEnum.RESCODE_SUCCESS.getCode());
        return result.success(data, resMsg);
    }

    public ResultVo success(T data, String resMsg) {
        return this.pack(data, resMsg, ErrorCodeEnum.RESCODE_SUCCESS.getCode());
    }

    public ResultVo fail() {
        return this.fail(ErrorCodeEnum.INTERNAL_SERVER_ERROR_CODE.getCode(), ErrorCodeEnum.INTERNAL_SERVER_ERROR_CODE.getCodeMsg());
    }

    public ResultVo fail(Exception e) {
        String message = e.getMessage();
        int index = message.indexOf(":");
        return this.fail(index == -1 ? message : message.substring(index + 1));
    }

    public static ResultVo failException(String resCode, Exception e) {
        ResultVo Result = newInstance();
        return Result.fail(resCode, e);
    }

    public ResultVo fail(String resCode, Exception e) {
        String message = e.getMessage();
        int index = message.indexOf(":");
        return this.fail(resCode, index == -1 ? message : message.substring(index + 1));
    }

    public static ResultVo failMsg(String errMsg) {
        ResultVo Result = newInstance();
        return Result.fail(errMsg);
    }

    public ResultVo fail(String errMsg) {
        return this.fail("05000", errMsg);
    }

    public static ResultVo failData(Object data, String resMsg) {
        ResultVo Result = newInstance();
        return Result.fail(data, resMsg);
    }

    public ResultVo fail(T data, String resMsg) {
        return this.pack(data, resMsg, "05000");
    }

    public static ResultVo failCodeMsg(String resCode, String resMsg) {
        ResultVo Result = newInstance();
        return Result.fail(resCode, resMsg);
    }

    public ResultVo fail(String resCode, String errMsg) {
        this.resMsg = errMsg;
        this.resCode = resCode;
        return this;
    }

    private ResultVo pack(T data, String resMsg, String resCode) {
        this.data = data;
        this.resMsg = resMsg;
        this.resCode = resCode;
        return this;
    }
}
